package com.example.es_promotion.repository;

import com.example.es_promotion.document.EsPromotion;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;

public interface EsPromotionRepository extends ElasticsearchRepository<EsPromotion, String> {
    
    List<EsPromotion> findByStatus(Integer status);
    
    @Query("{\"bool\": {\"must\": [{\"term\": {\"status\": 1}}, {\"range\": {\"startTime\": {\"lte\": \"?0\"}}}, {\"range\": {\"endTime\": {\"gte\": \"?0\"}}}]}}")
    List<EsPromotion> findActivePromotions(String currentTime);
    
    @Query("{\"bool\": {\"must\": [{\"term\": {\"status\": 1}}, {\"range\": {\"startTime\": {\"lte\": \"?0\"}}}, {\"range\": {\"endTime\": {\"gte\": \"?0\"}}}, {\"bool\": {\"should\": [{\"terms\": {\"applicableSkus\": ?1}}, {\"terms\": {\"applicableCategories\": ?2}}, {\"bool\": {\"must\": [{\"range\": {\"minPrice\": {\"lte\": ?3}}}, {\"range\": {\"maxPrice\": {\"gte\": ?3}}}]}}]}}]}}")
    List<EsPromotion> findMatchingPromotions(String currentTime, List<String> skuIds, 
                                           List<String> categoryIds, Double price);
}